package com.springTest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

@Configuration
public class LazyTest {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(LazyTest.class);
        System.out.println("----");
        System.out.println(context.getBean(F2.class));
        E e = context.getBean(E.class);
        System.out.println(e.getF3().getClass());
        System.out.println(e.getF3());
        context.close();
    }

    @Component
    static class E {
        @Autowired
        private F1 f1;

        @Lazy
        @Autowired
        private F3 f3;

        public F1 getF1() {
            return f1;
        }

        public F3 getF3() {
            return f3;
        }
    }

    @Lazy
    @Component
    static class F1 {
        public F1() {
            System.out.println("new F1()");
        }
    }

    @Lazy
    @Component
    static class F2 {
        public F2() {
            System.out.println("new F2()");
        }
    }

    static class F3 {
        public F3() {
            System.out.println("new F3()");
        }
    }

    @Configuration
    static class Config {
        @Bean
        @Lazy
        public F3 f3() {
            return new F3();
        }
    }
}
